//
//  setPwdViewController.m
//  ioschishan
//
//  Created by 千翼网络科技 on 2017/7/11.
//  Copyright © 2017年 qianyi. All rights reserved.
//

#import "setPwdViewController.h"

@interface setPwdViewController ()<UITextFieldDelegate>

@property (nonatomic,strong) UITextField *pwdTextField;
@property (nonatomic,strong) UITextField *pwdTextField1;
@property (nonatomic,strong) UIButton *nextBtn;

@end

@implementation setPwdViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initView];
}

#pragma mark ----------------------- init ---------------------

- (void)initView{
    
    self.title = @"设置密码";
    self.view.backgroundColor = getColor(@"f5f5f5");
    
    UIView *bgView = [[UIView alloc]initWithFrame:CGRectMake(0, screenScale(1), scrWidth(), screenScale(89))];
    bgView.backgroundColor = getColor(@"ffffff");
    [self.view addSubview:bgView];
    
    ///
    
    UILabel *pwdLabel = [ConfigXp labelWithFrame:CGRectMake(screenScale(15), screenScale(15), screenScale(50), screenScale(15))
                                            text:@"新密码"
                                       textColor:getColor(@"4a4a4a")
                                            font:get4sFont(12)
                                   textAlignment:NSTextAlignmentLeft
                                 backgroundColor:nil];
    [bgView addSubview:pwdLabel];
    
    _pwdTextField = [[UITextField alloc]init];
    _pwdTextField.placeholder = @"6-16位字母数字";
    _pwdTextField.delegate = self;
    _pwdTextField.secureTextEntry = YES;
    _pwdTextField.keyboardType = UIKeyboardTypeASCIICapable;
    _pwdTextField.returnKeyType = UIReturnKeyNext;
    _pwdTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
    _pwdTextField.font = get4sFont(12);
    [bgView addSubview:_pwdTextField];
    [_pwdTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.view).with.offset(screenScale(16));
        make.left.mas_equalTo(self.view).with.offset(screenScale(85));
        make.right.mas_equalTo(self.view).with.offset(screenScale(-15));
        make.height.mas_equalTo(screenScale(15));
    }];
    
    UIView *phoneThread = [[UIView alloc]initWithFrame:CGRectMake(screenScale(15), screenScale(44), screenScale(360), screenScale(0.5))];
    phoneThread.backgroundColor = getThreadColor();
    [bgView addSubview:phoneThread];
    
    ///
    
    UILabel *pwdLabel1 = [ConfigXp labelWithFrame:CGRectMake(screenScale(15), screenScale(60), screenScale(65), screenScale(15))
                                            text:@"重复密码"
                                       textColor:getColor(@"4a4a4a")
                                            font:get4sFont(12)
                                   textAlignment:NSTextAlignmentLeft
                                 backgroundColor:nil];
    [bgView addSubview:pwdLabel1];
    
    _pwdTextField1 = [[UITextField alloc]init];
    _pwdTextField1.placeholder = @"重复输入新密码";
    _pwdTextField1.delegate = self;
    _pwdTextField1.secureTextEntry = YES;
    _pwdTextField1.keyboardType = UIKeyboardTypeASCIICapable;
    _pwdTextField1.returnKeyType = UIReturnKeyNext;
    _pwdTextField1.clearButtonMode = UITextFieldViewModeWhileEditing;
    _pwdTextField1.font = get4sFont(12);
    [bgView addSubview:_pwdTextField1];
    [_pwdTextField1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.view).with.offset(screenScale(60));
        make.left.mas_equalTo(self.view).with.offset(screenScale(85));
        make.right.mas_equalTo(self.view).with.offset(screenScale(-15));
        make.height.mas_equalTo(screenScale(15));
    }];
    
    ///
    
    _nextBtn = [ConfigXp buttonWithFrame:CGRectMake(screenScale(20), screenScale(114), screenScale(335), screenScale(44))
                                   title:@"完成"
                              titleColor:getColor(@"ffffff")
                                    font:get4sFont(13)
                         backgroundColor:getMainColor()
                                   image:nil];
    _nextBtn.layer.cornerRadius = screenScale(3);
    [_nextBtn addTarget:self action:@selector(nextBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_nextBtn];
}

#pragma mark ----------------------- click ---------------------

- (void)nextBtnClick{
    
    if (![TSRegularExpressionUtil validatePassword:_pwdTextField.text] || ![TSRegularExpressionUtil validatePassword:_pwdTextField1.text]) {
        
        [PublicFunction showAlert:nil message:@"密码长度为6-16位" duration:1.0];
        return;
    }
    
    if ([_pwdTextField.text isEqualToString: _pwdTextField1.text] == NO) {
        
        [PublicFunction showAlert:nil message:@"密码不相同" duration:1.0];
        return;
    }
    
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:[[UIApplication sharedApplication]keyWindow] animated:YES];
    hud.tag = 500;
    //提示文本
    hud.labelText = @"正在加载...";
    //是否有遮罩
    hud.dimBackground = YES;
    
    NSMutableDictionary *productParams = [[NSMutableDictionary alloc]init];
    [productParams setValue:_phoneStr  forKey:@"phone"];
    [productParams setValue: _pwdTextField1.text forKey:@"newpass"];
    
    [[MNetworkHandler sharedInstance]conURL:login_resetPass
                                networkType:NetWorkPOST
                                     params:productParams
                                   delegate:nil
                                    showHUD:NO
                               successBlock:^(id returnData) {
                                   
                                   if ([[returnData objectForKey:@"code"]intValue] == 1) {
                                       
                                       [hud removeFromSuperview];
                                       [PublicFunction showAlert:nil message:[returnData objectForKey:@"msg"] duration:1.0f];
//                                       [PublicFunction showAlert:nil Mesasge:[returnData objectForKey:@"msg"] viewController:self];
                                       
                                       [self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:1] animated:YES];
                                       
                                   }else{
                                       
                                       [hud removeFromSuperview];
                                       [PublicFunction showAlert:nil message:[returnData objectForKey:@"msg"] duration:1.0f];
                                   }
                                   
                               } failureBlock:^(NSError *error) {
                                   
                                   [hud removeFromSuperview];
                                   NSLog(@"%@",error);
                                   [PublicFunction showAlert:nil message:@"没有连接网络" duration:1.0f];
                               }];
}

#pragma mark ----------------------- UITextFieldDelegate ---------------------

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    
    NSCharacterSet *cs;
    if (textField == _pwdTextField || textField == _pwdTextField1) {
        
        cs = [[NSCharacterSet characterSetWithCharactersInString:@"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"] invertedSet];
        NSString *filtered = [[string componentsSeparatedByCharactersInSet:cs] componentsJoinedByString:@""];
        BOOL basicTest = [string isEqualToString:filtered];
        if(!basicTest)  {
            return NO;
        }
        
        if (string.length == 0) return YES;
        
        NSInteger existedLength = textField.text.length;
        NSInteger selectedLength = range.length;
        NSInteger replaceLength = string.length;
        
        if (existedLength - selectedLength + replaceLength > 16) {
            
            return NO;
        }
    }
    
    return YES;
}

@end
